package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.dao.ParticipantDao;
import io.renren.entity.ParticipantEntity;
import io.renren.service.ParticipantService;
import io.renren.service.RoomEslService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("participantService")
public class ParticipantServiceImpl extends ServiceImpl<ParticipantDao, ParticipantEntity> implements ParticipantService {

    @Autowired
    private RoomEslService roomEslService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String eslId = (String) params.get("eslId");
        String applicationId = (String) params.get("applicationId");

        IPage<ParticipantEntity> page = this.page(
                new Query<ParticipantEntity>().getPage(params),
                new QueryWrapper<ParticipantEntity>()
                        .eq(applicationId != null, "application_id", applicationId)
                        .eq(eslId != null, "esl_id", eslId)

        );

        return new PageUtils(page);
    }

    public void bookApplication(Long roomId, Long applicationId, List<String> participants) {

        List<ParticipantEntity> participantEntities = new ArrayList<>();
        for (String participant : participants) {
            ParticipantEntity participantEntity = ParticipantEntity.create(applicationId, participant);
            participantEntities.add(participantEntity);
        }


//        Map<String, Object> map = new HashMap<>();
//        map.put("roomId", roomId);
//        map.put("type", "namePlate");
//        PageUtils pageUtils = roomEslService.queryPage(map);
//        List<RoomEslEntity> roomEslEntities = (List<RoomEslEntity>) pageUtils.getList();
//        if (CollectionUtil.isNotEmpty(roomEslEntities)) {
//            int aliasNum = roomEslEntities.size() % 2 + roomEslEntities.size() / 2;
//            Integer max = aliasNum > participantEntities.size() ? participantEntities.size() : aliasNum;
//            for (int i = 0; i < max; i++) {
//                int nextEslIndex = 2 * i + 1;
//                if (nextEslIndex < roomEslEntities.size()) {
//                    //一个与会人刷两个铭牌
//                    participantEntities.get(i).setEslId(roomEslEntities.get(i * 2).getEslId() + "," + roomEslEntities.get(nextEslIndex).getEslId());
//                } else {
//                    participantEntities.get(i).setEslId(roomEslEntities.get(i * 2).getEslId());
//                }
//
//            }
//        }

        this.saveBatch(participantEntities);

    }

}
